/***
 * Copyright (C) 2011  wexoo
 * p.weixlbaumer@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.wexoo.group_cost_calculator.proxy;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.wexoo.group_cost_calculator.enitities.Calculation;
import net.wexoo.group_cost_calculator.enitities.CalculationMember;
import net.wexoo.group_cost_calculator.enitities.DatabaseEntity;
import net.wexoo.group_cost_calculator.enitities.Person;
import net.wexoo.group_cost_calculator.util.Log;
import android.content.Context;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;

/**
 * OfflineORMLiteProxy.java
 * 
 * @author wexoo
 */
public class OfflineORMLiteProxy extends AbstractProxy {

   private static final String TAG = OfflineORMLiteProxy.class.getSimpleName();

   private static OrmLiteSqliteOpenHelper helper;

   public OfflineORMLiteProxy(Context context) {
      helper = OpenHelperManager.getHelper(context);
      Log.d(TAG, "Helper fetched by using HeurigenApp.mainContext");
   }

   /**
    * @see net.wexoo.group_cost_calculator.proxy.AbstractProxy#getAllCalculations()
    */
   @Override
   public List<Calculation> getAllCalculations() {
      try {
         Dao<Calculation, Integer> calculationsDao = helper.getDao(Calculation.class);

         List<Calculation> calculations = calculationsDao.queryForAll();

         for (Calculation calculation : calculations) {
            calculation.setMembers(getPersonsByCalculation(calculation.getId()));
         }

         return calculations;
      } catch (SQLException e) {
         Log.d(TAG, e.getMessage());
         e.printStackTrace();
      }
      return new ArrayList<Calculation>();
   }

   /**
    * @author wexoo
    * @since 1.0.0 10.09.2011
    */
   private List<Person> getPersonsByCalculation(Integer calcId) {
      List<Person> calculationMembers = new ArrayList<Person>();

      try {
         Dao<CalculationMember, Integer> calculationMemberDao = helper.getDao(CalculationMember.class);

         List<CalculationMember> calculationMemberList =
               calculationMemberDao.queryBuilder().where().eq(CalculationMember.CALCULATION_ID_COLUMN, calcId).query();

         for (CalculationMember calculationMember : calculationMemberList) {
            Dao<Person, Integer> personDao = helper.getDao(Person.class);

            calculationMembers.add(personDao.queryForId(calculationMember.getPersonId()));
         }
      } catch (SQLException e) {
         Log.d(TAG, e.getMessage());
         e.printStackTrace();
      }
      return calculationMembers;
   }

   /**
    * @see net.wexoo.group_cost_calculator.proxy.AbstractProxy#saveOrUpdateEntity(net.wexoo.group_cost_calculator.enitities.DatabaseEntity, java.lang.Class)
    */
   @Override
   public <T extends DatabaseEntity> void saveOrUpdateEntity(T entity, Class<T> entityClass) {
      try {

         Dao<T, Integer> entityDao = helper.getDao(entityClass);

         if (entity.getId() == null) {
            entityDao.create(entity);
         } else {
            entityDao.update(entity);
         }
      } catch (SQLException e) {
         Log.d(TAG, e.getMessage());
         e.printStackTrace();
      }
   }

   /**
    * @see net.wexoo.group_cost_calculator.proxy.AbstractProxy#saveOrUpdateEntities(java.util.List, java.lang.Class)
    */
   @Override
   public <T extends DatabaseEntity> void saveOrUpdateEntities(List<T> entities, Class<T> entityClass) {

      for (T entity : entities) {
         saveOrUpdateEntity(entity, entityClass);
      }
   }
}
